package com.dlsy.ygj.dao.vindicateData.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import net.taobaoge.common.framework.dao.impl.BaseDAOImpl;

import com.dlsy.ygj.dao.vindicateData.IFavouredPlanSectionDAO;
import com.dlsy.ygj.entity.FavouredPlanSection;

public class FavouredPlanSectionDAO extends BaseDAOImpl<FavouredPlanSection> implements IFavouredPlanSectionDAO{

	public void savePlanSections(final List<FavouredPlanSection> sections,final Integer fId) {
		getHibernateTemplate().execute(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				for (int i = 0; i < sections.size(); i++) {
					FavouredPlanSection section=new FavouredPlanSection();
					section.setBeginMoney((sections.get(i)).getBeginMoney());
					section.setEndMoney(sections.get(i).getEndMoney());
					section.setFavouredType(sections.get(i).getFavouredType());
					section.setFavoredTypeContext(sections.get(i).getFavoredTypeContext());
					section.setFavoredId(fId);
					session.save(section);
					if(i%5==0){
						session.flush();
						session.clear();
					}
					
				}
				return null;
			}
		});
		
	}

	@SuppressWarnings("unchecked")
	public List<FavouredPlanSection> findAllByPlanId(final Integer planId) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			
			public List<FavouredPlanSection> doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria criteria=session.createCriteria(FavouredPlanSection.class);
				criteria.add(Restrictions.eq("favoredId", planId));
				return criteria.list();
			}
		});
	}

	public void saveOrUpdateAll(List<FavouredPlanSection> sections) {
		super.saveOrUpdateAll(sections);
		
	}

}
